package com.niit.controller;



import com.niit.entity.Hall;
import com.niit.service.HallService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin(origins = "http://domain2.com", maxAge = 3600)
@RestController
@RequestMapping("/Hall")
public class HallController {

    @Resource
    private HallService HallService;

    @RequestMapping("/findAllHalls")
    public List<Hall> findAllHalls() {
        return HallService.findAllHalls();
    }

    @GetMapping("/findHallBy/{id}")
    public Hall queryById(@PathVariable("id") Integer id) {
        return this.HallService.findHallById(Long.valueOf(id));
    }

    @RequestMapping("/findHallByName")
    public Hall findHallByHall(@RequestBody Hall hall) {
        System.out.println("前端发送的数据:"+hall);
        String hall_name = hall.getHallName();
        Hall dbHall  = this.HallService.findHallByName(hall_name);
        if (dbHall != null) {
            return dbHall;
        }
        return dbHall;
    }

    @PostMapping("/addHall")
    public Hall addHall(@RequestBody Hall hall) {
        System.out.println("前端发送的数据:"+hall);
        return this.HallService.addHall(hall);
    }

    @RequestMapping("/updateHall")
    public String updateHall(@RequestBody Hall hall) {
        System.out.println("前端发送的数据:"+hall);
        int result = HallService.updateHall(hall);
        if (result >= 1) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }


    @RequestMapping("/deleteHall")
    public String deleteHall(@RequestBody Hall hall) {
        System.out.println("前端发送的数据:"+hall);
        Long id = hall.getHallId();
        System.out.println("前端发送数据id:"+id);
        Hall dbHallID = this.HallService.findHallById(id);
        System.out.println("数据库获取的id:"+dbHallID);
        if (dbHallID == null) {
            // 处理id为null的情况，比如返回错误信息或者重定向到一个错误页面
            return "请求的ID不存在";
        }
        int result = HallService.deleteHall(Math.toIntExact(dbHallID.getHallId()));
        if(result >= 1 )
        {
            return "删除成功";
        }
        else
        {
            return "删除失败";
        }
    }







}
